#!/bin/sh
#author charle
#time 2015-11-09

log_dir='/home/bestv/cmsdata/log/etl'
last_log_file=`ls -rt $log_dir| tail -n 1`
last_log_file_path=$log_dir/$last_log_file

hostname=127.0.0.1
username=root
password=mysql
dbname=recommendationengine
talbename=etl_log
select_sql="select * from ${talbename}"
ftp_log_file='ftp_log.html'

kettle_setting='/root/.kettle/kettle.properties'
ftp_server_viewlog=`more $kettle_setting |grep ftp_server_viewlog|sed 's/.*=//g'`
ftp_user_viewlog=`more $kettle_setting |grep ftp_user_viewlog|sed 's/.*=//g'`
ftp_password_viewlog=`more $kettle_setting |grep ftp_password_viewlog|sed 's/.*=//g'`
ftp_dir_viewlog=`more $kettle_setting |grep ftp_dir_viewlog|sed 's/.*=//g'`

ftp_url=ftp://$ftp_user_viewlog:$ftp_password_viewlog@$ftp_server_viewlog$ftp_dir_viewlog

echo '------------------当前ftp_url为----------------'
echo $ftp_url

echo '------------------日志处理结果------------------'
tail -n 20 $last_log_file_path

echo '------------------错误统计----------------------'
tail -n 20 $last_log_file_path|grep ERROR|wc

echo '--------选择是否继续执行修改数据库脚本-------------'  
select choose in "继续执行脚本" "放弃执行脚本";  
do  
    break  
done  

if [ $choose == "继续执行脚本" ];then
    echo  "正在执行数据脚本,请稍候....."
    mysql -h${hostname} -u${username} -p${password} ${dbname} -e "${select_sql}"
    continue
elif [ $choose = "放弃执行脚本" ];then
    echo "你选择放弃执行脚本......"
    exit
fi

echo '--------选择是否获取驻地ftp观看日志-------------'  
select choose in "继续执行脚本" "放弃执行脚本";  
do  
    break  
done  

if [ $choose == "继续执行脚本" ];then
    echo  "正在执行数据脚本,请稍候....."
    if [ -f $ftp_log_file ];then
        rm -rvf $ftp_log_file
    fi
    wget $ftp_url -O $ftp_log_file
    more $ftp_log_file|grep VLLOG|sed 's/.*zip">//g'|sed 's/<\/a.*//g'|tail -n 30
    continue
elif [ $choose = "放弃执行脚本" ];then
    echo "你选择放弃执行脚本......"
    exit
fi

echo '--------选择是否更新数据库的时间---------------'  
select choose in "继续执行脚本" "放弃执行脚本";  
do  
    break  
done  

if [ $choose == "继续执行脚本" ];then
    echo  "正在执行数据脚本,请稍候....."
    while read -p "请输入要更新的数据库时间: " time
    do
        echo $time
        update_sql="update etl_log set StatDate='$time' where ETLName='viewlog'"
        echo $update_sql
        break
    done
    mysql -h${hostname} -u${username} -p${password} ${dbname} -e "${update_sql}"
    continue
elif [ $choose = "放弃执行脚本" ];then
    echo "你选择放弃执行脚本......"
    exit
fi

echo '--------选择是否执行观看日志分析脚本---------------'  
select choose in "继续执行脚本" "放弃执行脚本";  
do  
    break  
done  

if [ $choose == "继续执行脚本" ];then
    echo  "正在执行数据脚本,请稍候....."
    while read -p "请输入要更新的天数" days
    do
        nohup /opt/data-integration/kitchen.sh -norep -file=/home/bestv/BMM_Modeling/JOB_Viewlog_Daily.kjb -param:days=$days &
        break
    done
    continue
elif [ $choose = "放弃执行脚本" ];then
    echo "你选择放弃执行脚本......"
    exit
fi
